package com.chidopi.market.bookstore.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

import com.chidopi.cdf.dao.AbstractBaseDaoImpl;
import com.chidopi.market.bookstore.domain.City2Category;

public class City2CategoryDaoImpl extends AbstractBaseDaoImpl<City2Category>  {
	
	private static Logger logger = LoggerFactory.getLogger(City2CategoryDaoImpl.class);
	
    @Override
	public int insert(City2Category t) {
		String sql = "insert into city2category (CITY_ID, CATEGORY_ID, ENABLE_FLAG, PARENT_ID, LEVEL ) "
				+ "values (:city_id, :category_id , :enable_flag , :parent_id, :level )";
		return super.execSQL(sql, t);
	}

	@Override
	public int update(City2Category t) {
		String sql = "update city2category set enable_flag = :enable_flag, parent_id = :parent_id, level = :level "
				+ "where city_id = :city_id and category_id = :category_id ";
		return super.execSQL(sql, t);
	}

	@Override
	public int delete(City2Category t) {
		String sql = "delete from city2category where city_id = :city_id and category_id = :category_id ";
		return super.execSQL(sql, t);
	}

	@Override
	public List<City2Category> find(String sqlWhere, Object... args) {
		if (sqlWhere == null || sqlWhere.trim().equals("")) {
			String sql = "select c.*, d.CATEGORYNAME PARENTNAME from (select a.*, b.CATEGORYNAME from city2category a inner join bookcategories b on a.CATEGORY_ID = b.id ) c left join bookcategories d on c.PARENT_ID = d.ID order by c.id ";
			return super.simpleJdbcTemplate.query(sql, this.getRowMapper());
		} else {
			logger.debug("sqlWhere:[" + sqlWhere + "]/params:" + Arrays.asList(args).toString());
			String sql = "select c.*, d.CATEGORYNAME PARENTNAME from (select a.*, b.CATEGORYNAME from city2category a inner join bookcategories b on a.CATEGORY_ID = b.id where " + sqlWhere + " ) c left join bookcategories d on c.PARENT_ID = d.ID order by c.id ";
			logger.debug("sql: " + sql);
			List<City2Category> list = super.simpleJdbcTemplate.query(sql, this.getRowMapper(), args);
			if (list == null) {
				logger.debug("query null!!!");
			} else {
				for (City2Category city2category : list) {
					logger.debug("id=" + city2category.getId()+ "/categoryName=" + city2category.getCategoryName());
				}
			}
			return list;
		}
	}

	@Override
	public City2Category findById(Object id) {
		
		return null;
	}

	@Override
	protected String getTable() {
		return "city2category";
	}

	@Override
	protected String getSequenceName() {
		return null;
	}

	@Override
	protected RowMapper<City2Category> getRowMapper() {
		return new ParameterizedRowMapper<City2Category>(){

			@Override
			public City2Category mapRow(ResultSet rs, int rowNum)	throws SQLException {
				City2Category vo = new City2Category();
				
				vo.setCity_id(rs.getInt("CITY_ID"));
				vo.setCategory_id(rs.getInt("CATEGORY_ID"));
				vo.setCategoryName(rs.getString("CATEGORYNAME"));
				vo.setParent_id(rs.getInt("PARENT_ID"));
                vo.setParent_name(rs.getString("PARENTNAME"));
                vo.setLevel(rs.getInt("LEVEL"));
				vo.setEnable_flag(rs.getString("ENABLE_FLAG"));
				return vo;
			}
		};	
    }

}
